package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke1.FriendinfoActivity;
import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.Intentnet;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import cn.xl.xxl.imagecache.ImageLoader;

public class AddfriendActivity extends Activity {

//	private Intent intent;
//	private String intExtra;
	private ImageView mIcio;
	private TextView mTphone,mTname;
	private ImageLoader imageLoader;
	private EditText mEcarno;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addfriend);
		imageLoader = new ImageLoader(this);
		net = new Intentnet();
		mIcio = (ImageView) findViewById(R.id.addf_image_inco);
		mTphone = (TextView) findViewById(R.id.addf_phone);
		mTname = (TextView) findViewById(R.id.addf_textname);
		mEcarno = (EditText) findViewById(R.id.search_edit);
		mEcarno.addTextChangedListener(watcher);
		relst = findViewById(R.id.search_relse);
		mBar = findViewById(R.id.searchadd_progress_bar);
		findViewById(R.id.addf_ok).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		findViewById(R.id.addactivity_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
//		getdata();
		
	}
	
	private TextWatcher watcher = new TextWatcher() {
	    @Override
	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    }
	    @Override
	    public void beforeTextChanged(CharSequence s, int start, int count,
	            int after) {
	    }
	    
	    @Override
	    public void afterTextChanged(Editable s) {
	    	String trim = mEcarno.getText().toString().trim();
	    	if(null != trim && trim.length() == 11){
	    		if(net.isMobile(trim) == false){
	    			Toast.makeText(AddfriendActivity.this, "请输入正确手机号", Toast.LENGTH_SHORT).show();
	    		}else{
	    			getdata(trim);
	    		}
	    	}
	    }
	};
	private Intentnet net;
	private View relst;
	private View mBar;

	private void getdata(String trim) {
		AsyncHttpClient ahc = new AsyncHttpClient();
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "add");
		params.put("phone", trim);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				Log.e("&**", ""+response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						JSONObject jsonObject = response.getJSONObject("data");
						String userid = jsonObject.getString("data");
						if(null != userid){
							Intent intent = new Intent(AddfriendActivity.this,FriendinfoActivity.class);
							intent.putExtra("userid", userid);
							intent.putExtra("closed", 1);
							startActivity(intent);
						}
					}else{
						Toast.makeText(AddfriendActivity.this, "未找到相关好友", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(AddfriendActivity.this, "未找到相关好友", Toast.LENGTH_SHORT).show();
			}
			@Override
			public void onStart() {
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}
			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}
}
